package com.itheima.health.service.impl;

import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 数据报表service
 */
@Service
@Slf4j
public class ReportServiceImpl implements ReportService {
    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    @Override
    public Map<Object,Object> getSetmealReport() {
        //定义体检预约套餐的唯一ID集合
        List<Integer> setmealIds =  orderDao.setmealIds();
        //定义预约套餐的名字集合
        List<String> setmealNames = new ArrayList<>();
        //存储所有数据
        Map<Object,Object> data = new HashMap<>();
        //定义存储全部套餐集合
        List<Object> setmealCount = new ArrayList<>();
        //遍历套餐的唯一ID集合
        for (Integer setmealId : setmealIds) {
            //存储每个套餐的预约数
            Map<String, Object> setmealMap = new HashMap<String, Object>();
            //获取预约套餐，预约数
           Integer countSetmeal = orderDao.countOrderADNSetmeal(setmealId);
            //获取套餐的名字
           String  nameSetmeal  = orderDao.setmealNameOrderADNSetmeal(setmealId);
            //往Map存储数据
            setmealMap.put("name",nameSetmeal);
            setmealMap.put("value",countSetmeal);
            //存储全部套餐的预约数
            setmealCount.add(setmealMap);
            //存储全部套餐集合
            setmealNames.add(nameSetmeal);
        }
        //存储全部套餐的预约数
        data.put("setmealCount",setmealCount);
        //存储全部套餐集合
        data.put("setmealNames",setmealNames);

        return data;
    }

    @Override
    public LinkedHashMap<String,Integer> getZhexianData() {
        LinkedHashMap<String,Integer> data =new LinkedHashMap<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY.MM.dd");
        SimpleDateFormat dateFormat1 = new SimpleDateFormat("YYYY.MM");
        //当前月

        //每次月数减一
        for (int i = 0; i <12; i++) {
            Calendar calendar=Calendar.getInstance();
            calendar.add(Calendar.MONTH,-12+i);
            Date date=calendar.getTime();
            String format = dateFormat.format(date);
            String format1 = dateFormat1.format(date);
            Integer monthCount = memberDao.monthMember(format);
            data.put(format1,monthCount);
        }
        return data;
    }

    @Override
    public Map<Object, Object> getBusinessReportData() {
        //存储所有数据
        Map<Object,Object> data = new HashMap<>();
        //定义体检预约套餐的唯一ID集合
        List<Integer> setmealIds =  orderDao.setmealIds();
        //定义存储全部套餐集合
        List< Map<String, Object>> setmealCount = new ArrayList<>();
        //取值有几个套餐
        int countMember = 0;
        for (Integer setmealId : setmealIds) {
            int countSetmeal = orderDao.countOrderADNSetmeal(setmealId);
            countMember+=countSetmeal;
        }
        //遍历套餐的唯一ID集合
        for (Integer setmealId : setmealIds) {
            //存储每个套餐的预约数
            Map<String, Object> setmealMap = new HashMap<String, Object>();
            //获取预约套餐，预约数
            double countSetmeal = orderDao.countOrderADNSetmeal(setmealId);
            double proportion =   countSetmeal/countMember;
            String Stringproportion = String.format("%.2f", proportion);
            //获取套餐的名字
            String  nameSetmeal  = orderDao.setmealNameOrderADNSetmeal(setmealId);
            //往Map存储数据
            setmealMap.put("proportion",Stringproportion);
            setmealMap.put("name",nameSetmeal);
            setmealMap.put("setmeal_count",countSetmeal);
            setmealCount.add(setmealMap);
        }
        //热门套餐进行顺序排序
        setmealCount.sort((o1, o2) -> {
            String name1 = (String) o1.get("proportion");
            String name2 = (String) o2.get("proportion");
            return name2.compareTo(name1);
        });
        //插入热门套餐
        data.put("hotSetmeal",setmealCount);
        //获取当前日期
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd");
        SimpleDateFormat MonthdateFormat = new SimpleDateFormat("YYYY-MM-01");
        //今日日期
        data.put("reportDate",dateFormat.format(date));
        //今日新增会员
        Integer todayNewMember = memberDao.todayNewMember(dateFormat.format(date));
        data.put("todayNewMember",todayNewMember);
        //本周新增会员
        Integer thisWeekNewMember = memberDao.thisWeekNewMember();
        data.put("thisWeekNewMember",thisWeekNewMember);
        //本月新增会员
        Integer thisMonthNewMember = memberDao.thisMonthNewMember(MonthdateFormat.format(date));
        data.put("thisMonthNewMember",thisMonthNewMember);
        //总会员
        Integer totalMember = memberDao.totalMember();
        data.put("totalMember",totalMember);
        //今日预约数
        Integer todayVisitsNumber = orderDao.todayVisitsNumber(dateFormat.format(date));
        data.put("todayVisitsNumber",todayVisitsNumber);
        //今日到诊人数
        Integer todayOrderNumber = orderDao.todayOrderNumber(dateFormat.format(date));
        data.put("todayOrderNumber",todayOrderNumber);
        //本周预约数
        Integer thisWeekVisitsNumber = orderDao.thisWeekVisitsNumber();
        data.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        //本周订单
        Integer thisWeekOrderNumber =orderDao.thisWeekOrderNumber();
        data.put("thisWeekOrderNumber",thisWeekOrderNumber);
        //本月预约数
        Integer thisMonthVisitsNumber = orderDao.thisMonthVisitsNumber(MonthdateFormat.format(date));
        data.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        //本月到诊数
        Integer thisMonthOrderNumber = orderDao.thisMonthOrderNumber(MonthdateFormat.format(date));
        data.put("thisMonthOrderNumber",thisMonthOrderNumber);

        log.info("[全部数据]date：{}",date);
        return data;
    }
}
